/*
 * To change this template, choose Tools | Templates and open the template in the editor.
 */

package org.dinnermate.validation;

// imports
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Color;
import javax.swing.BorderFactory;

/**
 * Used to validate a text box GUI component using the Validator class. The text box is highlighted and the user is
 * informed of invalid entries.
 * 
 * @author dinnermate
 */
public class TextBoxValidator extends Validator
{
	/**
	 * Constructor.
	 */
	public TextBoxValidator()
	{}

	/**
	 * Validates the text box provided with the Validator class and visually alerts the user of the invalid entry.
	 * 
	 * @param textBox
	 *            JTextField to be validated.
	 * @param type
	 *            (0) name, (1) email, (2) phone, (3) price (4) address (5) quantity
	 * @return 1 if failed.
	 */
	public int validateTextBox( JTextField textBox, int type )
	{
		String input = textBox.getText();

		// validator classes
		NameValidator nameVal = new NameValidator();
		EmailValidator emailVal = new EmailValidator();
		PhoneNumberValidator phoneVal = new PhoneNumberValidator();
		PriceValidator priceVal = new PriceValidator();
		AddressValidator addVal = new AddressValidator();
		QuantityValidator quantityVal = new QuantityValidator();

		// inform user if invalid
		switch (type)
		{
			case (0):
				if (!nameVal.validate(input))
				{
					inform(textBox);
					return 1;
				}
			break;
			case (1):
				if (!emailVal.validate(input))
				{
					inform(textBox);
					return 1;
				}
			break;
			case (2):
				if (!phoneVal.validate(input))
				{
					inform(textBox);
					return 1;
				}
			break;
			case (3):
				if (!priceVal.validate(input))
				{
					inform(textBox);
					return 1;
				}
			break;
			case (4):
				if (!addVal.validate(input))
				{
					inform(textBox);
					return 1;
				}
			break;
			case (5):
				if (!quantityVal.validate(input))
				{
					inform(textBox);
					return 1;
				}
			break;
			default:
				return 1;
		}
		unInform(textBox);
		return 0;
	}

	/**
	 * Pink highlight and border enhancement to alert user of the invalid entry.
	 * 
	 * @param textBox
	 *            JTextField that failed validation.
	 * @param saveButton
	 *            Button to disable.
	 */
	public void inform( JTextField textBox )
	{
		Color pink = new Color(255, 211, 174);
		textBox.setBackground(pink);
		textBox.setBorder(BorderFactory.createLineBorder(Color.red, 3));
	}

	/**
	 * Removes alert from text box.
	 * 
	 * @param textBox
	 *            JTextField that failed validation.
	 */
	public void unInform( JTextField textBox )
	{
		Color white = new Color(255, 255, 255);
		textBox.setBackground(white);
		textBox.setBorder(BorderFactory.createLoweredBevelBorder());
	}
}
